package com.nq.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nq.common.entity.IpoList;
import com.nq.common.entity.info.IpoInfo;
import com.nq.dao.stock.StIpoMapper;
import com.nq.stock.entity.StIpo;
import com.nq.stock.service.IStIpoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Locale;

/**
 * @Description: st_ipo
 * @Author: jeecg-boot
 * @Date:   2024-06-05
 * @Version: V1.0
 */
@Service
public class StIpoServiceImpl extends ServiceImpl<StIpoMapper, StIpo> implements IStIpoService {

    @Resource
    private StIpoMapper stIpoMapper;

    @Override
    public int addIpo(IpoList ipos) {
        if (null == ipos) {
            return 0;
        }

        List<IpoInfo> data = ipos.getData();
        if (null == data || data.isEmpty()) {
            return 0;
        }

        for (IpoInfo ipo : data) {
            DateTimeFormatter originalFormat = DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.ENGLISH);
            LocalDate openDate = LocalDate.parse(ipo.getOpenDate(), originalFormat);
            LocalDate closeDate = LocalDate.parse(ipo.getCloseDate(), originalFormat);
            LocalDate listingDate = LocalDate.parse(ipo.getListingDate(), originalFormat);
            ipo.setOpenDate(openDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            ipo.setCloseDate(closeDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            ipo.setListingDate(listingDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            // 去掉千分位
            ipo.setIssueSize(removeThousandSeparator(ipo.getIssueSize()));
            ipo.setLotSize(removeThousandSeparator(ipo.getLotSize()));

            String issuePrice = ipo.getIssuePrice();
            if (issuePrice.contains("to")) {
                String before = issuePrice.substring(0, issuePrice.indexOf("to") - 1).trim();
                String after = issuePrice.substring(issuePrice.indexOf("to") + 2, issuePrice.length()).trim();
                BigDecimal avg = new BigDecimal(before).add(new BigDecimal(after)).divide(new BigDecimal("2")).setScale(2, 4);
                ipo.setIssuePrice(String.valueOf(avg));
            }

            StIpo addObj = new StIpo();
            BeanUtils.copyProperties(ipo, addObj);
            // 判断 ipo 是否存在
            QueryWrapper<StIpo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", ipo.getName());
            StIpo stIpo = stIpoMapper.selectOne(queryWrapper);
            if (null != stIpo) {
                addObj.setId(stIpo.getId());
                stIpoMapper.updateById(addObj);
            } else {
                stIpoMapper.insert(addObj);
            }
        }

        return 1;
    }


    private String removeThousandSeparator(String thousandSeparated){
        NumberFormat format = NumberFormat.getInstance();
        try {
            String withoutSeparator = format.parse(thousandSeparated).toString();
            return withoutSeparator;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "0";
    }

}
